<template>
  <view class="content">
    <!-- 收货地址部分，增加了类名和更清晰的结构 -->
    <view class="conaddress">
      <view>
        <view class="address-title">收货地址</view>
        <view class="address-details">
          {{ selectedAddress ? selectedAddress.city + selectedAddress.department + selectedAddress.houseNumber : '仓前区良睦路2888号梦想小镇2号楼10号' }}
        </view>
        <view class="contact-info">
          <view class="contact-name" style="padding-right: 20px;">
            {{ selectedAddress ? selectedAddress.name : '小兰' }}
          </view>
          <view class="contact-phone">
            {{ selectedAddress ? selectedAddress.phone : '13456789009' }}
          </view>
        </view>
      </view>
      <view><img src="/static/imgs/addressme.png" alt="" height="30px" @click="clickaddress"></view>
    </view>
    <!-- 订单详情部分，增加了类名和更清晰的结构 -->
    <view class="order">
      <view class="supermarket-title">{{ supermarketName }}</view>
      <view v-for="(item, index) in selectedItems" :key="index" class="order-item">
        <view class="item">
          <img :src="item.imgUrl" alt="" width="80px" height="80px">
        </view>
        <view class="item-message">
          <view class="item-name">{{ item.name }}</view>
          <view class="item-price">单价: ￥{{ item.price.toFixed(2) }}</view>
          <view class="item-quantity">数量: {{ item.quantity }}</view>
          <view class="item-subtotal">小计: ￥{{ (item.price * item.quantity).toFixed(2) }}</view>
        </view>
      </view>
      <view class="total-price">总价: ￥{{ calculateTotalPrice().toFixed(2) }}</view>
      <button @click="pay" class="pay-button">立即支付</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      selectedItems: [],
      supermarketName: '',
      selectedAddress: null
    };
  },
  onShow() {
    const selectedItemsData = uni.getStorageSync('selectedCheckoutItems');
    if (selectedItemsData) {
      try {
        this.selectedItems = JSON.parse(selectedItemsData);
        if (this.selectedItems.length > 0) {
          this.supermarketName = this.selectedItems[0].shopName;
        }
      } catch (error) {
        console.error('解析商品信息失败', error);
      }
    }
    const address = uni.getStorageSync('selectedAddress');
    if (address) {
      try {
        this.selectedAddress = JSON.parse(address);
      } catch (error) {
        console.error('解析地址信息失败', error);
      }
    }
  },
  methods: {
    //地址选择
    clickaddress() {
      uni.navigateTo({
        url: `/pages/own/myaddress`
      });
    },
    calculateTotalPrice() {
      return this.selectedItems.reduce((total, item) => {
        return total + item.price * item.quantity;
      }, 0);
    },
    pay() {
      uni.showModal({
        title: '提示',
        content: '确定要支付吗？',
        confirmText: "确认支付",
        cancelText: "取消订单",
        success: (res) => {
          if (res.confirm) {
            // 准备订单数据
            const orderData = {
              items: this.selectedItems,
              totalPrice: this.calculateTotalPrice(),
              shopName: this.supermarketName,
              address: this.selectedAddress
            };

            // 跳转到支付页面并传递订单数据
            uni.navigateTo({
              url: `/pages/orders/pay?data=${encodeURIComponent(JSON.stringify(orderData))}`
            });
          }
        }
      });
    },
    clearCheckedItems() {
      const cartData = uni.getStorageSync('cartlist');
      if (cartData) {
        try {
          let cartlist = JSON.parse(cartData);
          const shopId = this.selectedItems[0].shopId;
          cartlist = cartlist.filter(item => {
            return item.shopId!== shopId ||!item.selected;
          });
          uni.setStorageSync('cartlist', JSON.stringify(cartlist));
        } catch (error) {
          console.error('清除购物车失败', error);
        }
      }
    }
  }
};
</script>
<style scoped>
/* 整体页面样式，设置背景色和内边距 */
.content {
  background-color: #f4f4f4;
  padding: 20px;
}
/* 标题样式，设置背景色、文本颜色、内边距和圆角 */
.title {
  background-color: #007AFF;
  color: white;
  padding: 15px;
  border-radius: 10px;
  text-align: center;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.title h2 {
  margin: 0;
  font-size: 24px;
}
/* 收货地址容器样式，设置背景色、边框、圆角和内边距 */
.conaddress {
  background-color: white;
  border: 1px solid #e0e0e0;
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
/* 收货地址标题样式，设置字体大小和加粗 */
.address-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}
/* 收货地址详细信息样式，设置颜色和边距 */
.address-details {
  color: #666;
  margin-bottom: 10px;
}
/* 联系人信息容器样式，设置显示方式和边距 */
.contact-info {
  display: flex;
  /* justify-content: space-between; */
  
  align-items: center;
}
/* 联系人姓名样式，设置字体大小 */
.contact-name {
  font-size: 16px;
}
/* 联系人电话样式，设置字体大小和颜色 */
.contact-phone {
  font-size: 16px;
  color: #666;
}
/* 订单容器样式，设置背景色、边框、圆角和内边距 */
.order {
  background-color: white;
  border: 1px solid #e0e0e0;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
/* 超市名称样式，设置字体大小和加粗 */
.supermarket-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 15px;
}
/* 订单商品项样式，设置边框、圆角、内边距和外边距 */
.order-item {
  border: 1px solid #e0e0e0;
  border-radius: 10px;
  padding: 15px;
  margin-bottom: 15px;
  display: flex;
  align-items: flex-start;
}
/* 商品图片容器样式，设置外边距 */
.item {
  margin-right: 15px;
}
/* 商品信息容器样式，设置显示方式和宽度 */
.item-message {
  flex: 1;
  display: flex;
  flex-direction: column;
}
/* 商品名称样式，设置字体大小和加粗 */
.item-name {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 5px;
}
/* 商品单价、数量、小计样式，设置颜色和边距 */
.item-price,
.item-quantity,
.item-subtotal {
  color: #666;
  margin-bottom: 3px;
}
/* 总价样式，设置字体大小、加粗和文本对齐方式 */
.total-price {
  font-size: 18px;
  font-weight: bold;
  text-align: right;
  margin-top: 20px;
  margin-bottom: 15px;
}
/* 支付按钮样式，设置背景色、文本颜色、内边距、圆角和过渡效果 */
.pay-button {
  background-color: #1aad19;
  color: white;
  padding: 15px;
  border-radius: 10px;
  text-align: center;
  font-size: 14px;
  cursor: pointer;
  width: 30%;
  height: 50px;
  line-height: 25px;
  transition: background-color 0.3s ease;
}
.pay-button:hover {
  background-color: #168c13;
}
</style>    